package code.BinaryTree;

import struct.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class SimilarTree {
	public static void main(String[] args) {

	}

	//872. 叶子相似的树
	public boolean leafSimilar(TreeNode root1, TreeNode root2) {
		List<Integer> leaves1 = new ArrayList();
		List<Integer> leaves2 = new ArrayList();
		leafSimilarHelper(root1, leaves1);
		leafSimilarHelper(root2, leaves2);
		return leaves1.equals(leaves2);
	}

	public void leafSimilarHelper(TreeNode node, List<Integer> leafValues) {
		if (node == null) {
			return;
		}
		if (node.left == null && node.right == null) {
			leafValues.add(node.val);
		}
		leafSimilarHelper(node.left, leafValues);
		leafSimilarHelper(node.right, leafValues);
	}
}
